using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Cysharp.Threading.Tasks;
using DG.Tweening;
using Enume;
using SO;
using Struct;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Serialization;

namespace Entity {

    public partial class EntityController {

        public List<FightBuffData> FightBuffDatas = new List<FightBuffData>();

        /// <summary>
        /// buff所带来的属性附加 结束后按照这些属性进行移除
        /// </summary>
        private List<PropertiesStruct> buffProperties = new List<PropertiesStruct>();


        public void AddBuff(FightBuffData buffData) {
            //检查是否有相同buff
            FightBuffData firstBuff = FightBuffDatas.FirstOrDefault(x => x.buffType == buffData.buffType);
            if (firstBuff != null) {
                firstBuff.remainingRounds += buffData.remainingRounds;
            } else {
                FightBuffDatas.Add(buffData);
            }

            buffData.addCallback?.Invoke();
            foreach (PropertiesStruct propertiesStruct in buffData.propertiesList) {
                FightProperties.AddProperty(propertiesStruct.propertiesType, propertiesStruct.value);
                //添加buff属性到buffProperties
                //检查 buffProperties 是否有相同属性
                PropertiesStruct firstProperties = buffProperties.FirstOrDefault(x => x.propertiesType == propertiesStruct.propertiesType);
                if (firstProperties.IsNotNull()) {
                    firstProperties.value += propertiesStruct.value;
                } else {
                    buffProperties.Add(propertiesStruct);
                }
            }

            BuffRefresh();
        }

        public void AddBuff(EnumBuffType skillId, int remainingRounds, List<PropertiesStruct> propertiesList, Action removeCallback, Action action) {
            FightBuffData buffData = new FightBuffData(FightBuffManager.Instance.GetBuffItemConfig(skillId)?.buffName, skillId, remainingRounds);
            buffData.propertiesList = propertiesList;
            AddBuff(buffData);
        }

        public void AddBuff(EnumBuffType enumBuffType, int remainingRounds) {
            FightBuffData buffData = new FightBuffData(FightBuffManager.Instance.GetBuffItemConfig(enumBuffType)?.buffName, enumBuffType, remainingRounds);
            AddBuff(buffData);
        }


        public FightBuffData GetBuff(EnumBuffType buffType) {
            FightBuffData firstBuff = FightBuffDatas.FirstOrDefault(x => x.buffType == buffType);
            return firstBuff;
        }

        public void RemoveBuff(EnumBuffType buffType) {
            FightBuffData firstBuff = FightBuffDatas.FirstOrDefault(x => x.buffType == buffType);
            if (firstBuff != null) {
                firstBuff?.removeCallback?.Invoke();
                ClearBuffAddProperty();
                FightBuffDatas.Remove(firstBuff);
            }
        }

        public void RemoveAllBuffs() {
            FightBuffDatas.ForEach(e => {
                ClearBuffAddProperty();
                e.removeCallback?.Invoke();
            });
            FightBuffDatas.Clear();
            BuffRefresh();
        }

        /// <summary>
        /// 清除Buff所附带的属性
        /// </summary>
        /// <param name="propertiesList"></param>
        private void ClearBuffAddProperty() {
            foreach (PropertiesStruct propertiesStruct in buffProperties) {
                FightProperties.AddProperty(propertiesStruct.propertiesType, -propertiesStruct.value);
            }
            buffProperties.Clear();
        }

        /// <summary>
        /// buff是否处于激活状态 如果是  返回true 否则返回false
        /// </summary>
        /// <param name="buffType"></param>
        /// <returns></returns>
        public bool BuffIsActive(EnumBuffType buffType) {
            FightBuffData firstBuff = FightBuffDatas.FirstOrDefault(x => x.buffType == buffType);
            if (firstBuff != null) {
                return firstBuff.remainingRounds > 0;
            }

            return false;
        }

        /// <summary>
        /// 所有buff减少 1个回合
        /// </summary>
        public void BuffReduceRound() {
            // 创建一个临时列表来存储需要移除的 Buff
            List<FightBuffData> buffsToRemove = new List<FightBuffData>();
            // 遍历 FightBuffDatas 列表
            FightBuffDatas.ForEach(e => {
                e.remainingRounds--;
                if (e.remainingRounds == 0) {
                    // 将需要移除的 Buff 添加到临时列表中
                    buffsToRemove.Add(e);
                }
            });
            // 移除临时列表中的 Buff
            buffsToRemove.ForEach(e => RemoveBuff(e.buffType));
            BuffRefresh();
        }


        public void BuffRefresh() {
            CharacterCanvas.Go_Buffs.RefreshBuffs();
        }




    }

}